﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CorsatAdminApp.Library.TempTaskClasses;

namespace CorsatAdminApp.Desktop.TempTaskForms
{
    public partial class Absence : Form
    {
        public Absence()
        {
            InitializeComponent();
        }
        #region private Variable

        Absence_Administration Ab = new Absence_Administration();
        int DataGridCount;

        #endregion

        #region private Methods

        private void GridVeiwFunction()
        {
            try
            {

                AbsenceDataG.Columns.Add(new DataGridViewLinkColumn
                {
                    CellTemplate = new DataGridViewLinkCell(),
                    HeaderText = "م",

                });
                AbsenceDataG.Columns.Add(new DataGridViewLinkColumn
                {
                    CellTemplate = new DataGridViewLinkCell(),
                    HeaderText = "الاسم",

                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_1",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_2",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_3",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_4",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_5",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_6",
                });
                AbsenceDataG.Columns.Add(new DataGridViewCheckBoxColumn
                {
                    CellTemplate = new DataGridViewCheckBoxCell(),
                    HeaderText = "Att_7",
                });
            }
            catch (Exception)
            { MessageBox.Show("Error"); }
        }


        #endregion

        private void Absence_Load(object sender, EventArgs e)
        {
            GridVeiwFunction();

            CourseTitlesCB.DataSource = Ab.GetAllCourses();
            CourseTitlesCB.DisplayMember = "ItemName";
            CourseTitlesCB.ValueMember = "ItemID";
        }

        private void CourseTitlesCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadBt.Enabled = true;
            SaveBt.Enabled = false;


            AbsenceDataG.DataSource = null;
            AbsenceDataG.Rows.Clear();
            AbsenceDataG.Columns.Clear();

            GridVeiwFunction();
        }

        private void loadBt_Click(object sender, EventArgs e)
        {
            Ab.Absence.Clear();

            // Add Test Rows to the DataTable
            Ab.Absence.Rows.Add(1, "Mariam", false, false, false, false, false, false, false);
            Ab.Absence.Rows.Add(2, "joe", false, false, false, false, true, false, false);
            Ab.Absence.Rows.Add(3, "koo", false, false, true, false, false, false, false);

            // ---------------------------------------------------------------------------------
            try
            {
                Ab.CourseTitle.ItemName = Convert.ToString(CourseTitlesCB.SelectedValue);
                Ab.CourseLevel = Convert.ToInt32(levelCB.SelectedValue);
                
                // Data Grid
                AbsenceDataG.DataSource = null;
                AbsenceDataG.Rows.Clear();
                AbsenceDataG.Columns.Clear();

                // Set the Data Source of the Grid to the DataTable from the class.
                AbsenceDataG.DataSource = Ab.Absence;

                loadBt.Enabled = false; // ?
                SaveBt.Enabled = true;
            }
            catch (Exception) { MessageBox.Show("Error !"); }
        }

        private void SaveBt_Click(object sender, EventArgs e)
        {

            try
            {
                // Set the Course Title.
                // Note: Set the whole ITEM.
                Ab.CourseTitle.ItemName = CourseTitlesCB.SelectedValue.ToString();

                // Set the Course Level.
                Ab.CourseLevel = Convert.ToInt32(levelCB.SelectedValue);

                // Catch the Number of Rows to loop on.
                DataGridCount = AbsenceDataG.Rows.Count - 1;

                AbsenceDataG.DataSource = null;

                for (int i = 0; i < DataGridCount; i++)
                {
                    for (int j = 0; j < AbsenceDataG.Columns.Count; j++)
                    {
                        // Add the Rows to Save.
                        Ab.Absence.Rows.Add(
                            Convert.ToInt32(AbsenceDataG.Rows[i].Cells[j].Value), //ID
                            Convert.ToString(AbsenceDataG.Rows[i].Cells[j].Value),  // Full Name
                            Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value), // Day 1 (Att_1)
                            Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value), // Day 2 (Att_2)
                            Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value), // Day 3 (Att_3)
                            Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value), // Day 4 (Att_4)
                            Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value), // Day 5 (Att_5)
                            Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value), // Day 6 (Att_6)
                            Convert.ToBoolean(AbsenceDataG.Rows[i].Cells[j].Value) // Day 7 (Att_7)
                            );
                    }
                }

                AbsenceDataG.DataSource = Ab.Absence; //?

                Ab.Save();

            }
            catch (Exception) { MessageBox.Show("Please,Select CourseTitle & Level be4 Save ur Data"); }
        }
    }
}
